 <?php
include 'base.php';
include 'require_seller.php';
include 'connectdb.php';

function insert_bill($invoice_code, $product_code, $quantity, $customer_code, $seller_code, $discount, $date){
    $sql1 = "SELECT sale_price from product WHERE code = '".$product_code."'";
    $prices = mysql_fetch_array(mysql_query($sql1));
    $price = $prices['sale_price'];
    $total = ($price*$quantity*(100 - $discount))/100;
    
    $sql="INSERT INTO bill(invoice_code, product_code, price, quantity, customer_code, seller_code, discount, date, total)VALUES('$invoice_code', '$product_code', '$price', '$quantity', '$customer_code', '$seller_code', '$discount', '$date', '$total')";    
    return mysql_query($sql);
}

function get_invoice(){
    $sql = "SELECT max(invoice_code) from bill";
    $result = mysql_query($sql);
    while($row = mysql_fetch_array($result))
        return $row['max(invoice_code)'] + 1;
}

if($_POST){    
    if (insert_bill($_POST[invoice_code], $_POST[product_code], $_POST[quantity], $_POST[customer_code], $_POST[seller_code], $_POST[discount], $_POST[date])){    
        echo "Đã thêm thành công!";
    }
    else{                  
            header("HTTP/1.0 404 Not Found");
            header('HTTP', true, 500);
        }                
}
?>

<script language="JavaScript" type="text/javascript">
    $(document).ready(function () {
        $('#success, #error, #import_form').hide();    
        $("#date" ).datepicker();
        var nClick = 1;
        
        $("#finish").click(function(){
            if (confirm("Bạn có chắc chắn muốn kết thúc?")){
                window.location.href = 'hoa_don_seller.php';
            }
        });

        $("#discount").change(function(){
            if ($(this).val() > 100){
                $(this).val(100);
            }

            if ($(this).val() < 0){
                $(this).val(0);
            }
        });

        $("#product_code").change(function(){            
            $.ajax({
                type: "post",
                url: "get_product_info.php",
                data: {product_code :  $('#product_code').val()},
                dataType: "json",
                success: function(data)
                {                     
                        $("#price").val(data.price);
                },
                error: function()
                {                     
                        alert('Có lỗi xảy ra, kiểm tra lại thông tin') ;
                }               

            }); 
            return false;
        });

        $("#submit").click(function(){           
            if($("#product_code").val() == ''|| $("#quantity").val() == '' || $("#customer_code").val() == '' || $("#seller_code").val() == '' || $("#date").val() == '' || $("#discount").val() == ''){
                alert("Chưa điền đủ thông tin");
                return false;
            }

            var product_code = $('#product_code').val(),
                quantity = $('#quantity').val(),
                price = $('#price').val(),
                customer_code = $('#customer_code').val(),
                seller_code = $('#seller_code').val(),                            
                date = $('#date').val(),                            
                discount = $('#discount').val(),
                invoice_code = $("#invoice_code").val(),
                total = price*quantity*(100-discount)/100;                    
            $("#total").val(total);
            alert(invoice_code);
            $.ajax({
                type: "post",
                url: "",
                data: "product_code=" + product_code + "&quantity=" + quantity + "&customer_code=" + customer_code + "&seller_code=" + seller_code + "&date=" + date +  "&discount=" + discount + "&invoice_code=" + invoice_code,
                success: function () {                    
                    $('#error').hide();                
                    $('#success').show();
                    $("#total_invoice").val( $("#total_invoice").val() + total);
                    var newRow = $("<tr><td>" + product_code + "</td><td>" + price + "</td><td>" + quantity + "</td><td>" + discount + "</td><td>" + total + "</td></tr>");
                    $("#list_table").append(newRow);
                    
                },
                error: function() {                    
                    $('#error').show();  
                    $('#success').hide();                    
                }                
            }); 
            return false;

        });
    });
    
</script>

<div id ="info">                        
        <span>
            Mã Hóa đơn
            <input id = "invoice_code" disabled = "disabled" value="<?php echo get_invoice();?>">
            Mã khách hàng
            <select id = "customer_code">
                <option value="" selected = "selected">Chọn</option>
                <?php
                $stSQL="select code from customer";
                $resultsub=mysql_query($stSQL);           
                if(mysql_num_rows($resultsub)> 0)            
                    while($row = mysql_fetch_array($resultsub))
                    {
                    ?>          
                        <option value="<?php echo"".$row['code']."";?>"><?php echo"".$row['code']."";?></option>
                    <?php
                    }            
                    ?>
            </select>
        </span>
        <span>
            Mã nhân viên<input id = "seller_code" value="<?php echo $_COOKIE['name'];?>" disabled="disabled" />
        </span>
        <span>
            Ngày giao dịch <input type="text" name="date" id = "date" value="<?php echo date("Y-m-d"); ?>">
        </span>
        <span>
            Tổng tiền <input type="text" name="date" id = "total_invoice" disabled="disabled" value="0">
        </span>    
</div>
<table id = "list_table" class="table" style="table-layout:fixed; " >    
    <thead>        
    <tr>                    
        <th>Mã sản phẩm </th>
        <th>Giá sản phẩm</th>
        <th>Số lượng</th>        
        <th>Giảm giá</th>                    
        <th>Tổng tiền</th>
        <td></th>    
                
    </tr>
    </thead>
    <tbody>
    </tbody>
    <tr class="form">                
        <td><select id = "product_code">
            <option value="" selected = "selected">Chọn</option>
            <?php
            $stSQL="select code from product";
            $resultsub=mysql_query($stSQL);           
            if(mysql_num_rows($resultsub)> 0)            
                while($row = mysql_fetch_array($resultsub))
                {
                ?>          
                    <option value="<?php echo"".$row['code']."";?>"><?php echo"".$row['code']."";?></option>                    
                <?php
                }            
                ?>
        </select></td>
        <td><input type="text" name="price" id = "price" disabled = "disabled"></td>
        <td><input  title="Số lượng sản phẩm, nhập số nguyên lớn hơn 0" type="text" name="quantity" id = "quantity"></td>
                
        <td><input title="Phần trăm giảm giá, nhập số nguyên trong khoảng [0, 100]" value = "0" type="text" name="discount" id = "discount"></td>        
        <td><input type="text" name="total" id = "total" disabled="disabled"></td>                                                  
    </tr>

    <tr>        
        <td colspan="5">
            <div id = "error">Có lỗi xảy ra, thông tin nhập chưa đúng</div>
            <div id = "success">Đã thêm thành công</div>
            <span>
                <input class="submit" type="submit" id = "submit" value="Thêm hàng" />
                <input class="submit" type="submit" id = "finish" value="Kết thúc hóa đơn" />
            </span>
        </td>        
    </tr>    
</table>         
<?php include 'footer.php';?>